package com.example.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

/**
 * Description:
 * User: 故我
 * Date: 2023-10-28
 * Time: 21:08
 */
@Controller //这个是控制器
@ResponseBody  //表示返回的是一个数据，并非页面
@PropertySource(value = "application.properties", encoding = "utf-8")
public class TestController {
    @Value("${mytest}") // “” 这个只会获得key值， ${} 这个会获取value
    private String mytest;

    @Value("${mytest2}")
    private String mytest2;

    @Value("${myString1}")
    private String mystring1;

    @Value("${myString2}")
    private String mystring2;

    @Value("${myString3}")
    private String mystring3;

    //将对象获取出来
    @Autowired
    private Student student;

    //将集合获取出来
    @Autowired
    private ListConfig listConfig;

    @PostConstruct
    public void testController() {
        System.out.println(listConfig.getName());
        //System.out.println(student);
//        System.out.println("myString1: " + mystring1);
//        System.out.println("myString2: " + mystring2);
//        System.out.println("myString3: " + mystring3);
    }

    @RequestMapping("/hello") //请求的一个映射
    public String getName(String name) {
        if (!StringUtils.hasLength(name)) {
            //如果没有 name 长度
            name = "张三";
        }
        return "hello: " + name;
    }

    @RequestMapping("/getconfig")
    public String getConfig() {
        return "mytest: " + mytest + "  | mytest2: " + mytest2;
    }
}
